package com.frame.work.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.frame.work.entity.TblMessage;
import com.frame.work.entity.dto.TblMessageDTO;
import com.frame.work.service.TblMessageService;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 推送消息日志
 *
 * @author hewenze
 * @since
 */
@RestController
@RequestMapping("/api/message")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MessageController extends ApiController {
    /**
     * 服务对象
     */


    @Autowired
    private TblMessageService tblMessageService;


    /**
     * 通过时间查询
     *
     * @return
     */
    @GetMapping("/user")
    public R selectLog(@RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("messageType") String messageType,
                       @RequestParam("userId") String userId) {
        if (pageNum == 0) {
            pageNum = 0;
        } else {
            pageNum = (pageNum - 1) * pageSize;
        }

        pageSize = pageNum + pageSize;

        List<TblMessage> tblMessages = tblMessageService.selectByTime(pageNum, pageSize, messageType, userId);

        List<TblMessageDTO> tblMessageDTOS = new ArrayList<>();
        for (TblMessage tblMessage : tblMessages) {
            TblMessageDTO tblMessageDTO = new TblMessageDTO();
            BeanUtils.copyProperties(tblMessage, tblMessageDTO);
            tblMessageDTOS.add(tblMessageDTO);
        }


        return success(tblMessageDTOS);


    }

    /**
     * 查询系统未读消息数(新)
     *
     * @return
     */
    @GetMapping("/unReadSystemMessageCount")
    public R selectUnReadSystemMessageCount(
            @RequestParam("messageType") String messageType,
            @RequestParam("userId") String userId) {
        int count = tblMessageService.getUnReadSystemMessageCount(messageType, userId);
        return success(count);
    }

    /**
     * 新增推送消息
     *
     * @return
     */
    @PostMapping("/add")
    public R insertTblMessage(@RequestBody TblMessage tblMessage) {
        this.tblMessageService.insert(tblMessage);
        return success(null);
    }

    /**
     * 新增推送消息
     *
     * @return
     */
    @PostMapping("/manageAdd")
    public R insertTblMessageOfManage(@RequestParam("sender") String sender,
                                      @RequestParam("sendType") String sendType,
                                      @RequestParam("messageType") String messageType,
                                      @RequestParam("content") String content, String userId, String title

    ) {
        TblMessage tblMessage = new TblMessage();
        tblMessage.setSender(sender);
        tblMessage.setSendType(sendType);
        tblMessage.setMessageType(messageType);
        tblMessage.setContent(content);
        tblMessage.setUserId(userId);
        tblMessage.setTitle(title);

        this.tblMessageService.insert(tblMessage);
        return success(null);
    }


    /**
     * 根据用户id删除全部日志
     *
     * @return
     */

    @DeleteMapping("/deleteAll/userId")
    public String deleteAllTblMessageByUserId(@RequestParam("userId") String userId, String messageType) throws Exception {
        int i = this.tblMessageService.deleteAllByUserId(userId, messageType);
        if (i > 0) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }


    /**
     * 根据用户id修改消息为已读
     *
     * @param userId
     * @param messageType 8 系统消息
     * @return
     * @throws Exception
     */
    @PutMapping("/updateHasRead")
    public String updateMessageReadByUserId(@RequestParam("userId") String userId, @RequestParam("messageType") String messageType) {

        UpdateWrapper<TblMessage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("has_read", "1").eq("user_id", userId).eq("message_type", messageType).eq("has_read", "0");
        boolean updateFlag = tblMessageService.update(updateWrapper);

        if (updateFlag) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    /**
     * 根据日志id批量删除日志
     *
     * @return
     */
    @DeleteMapping("/delete/ids")
    public String deleteTblMessageByUserId(@RequestBody List<String> ids) {
        int i = this.tblMessageService.deleteByIds(ids);
        if (i > 0) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

}
